﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RobotJog.Controls.Joints.ExternalJointsLayoutBuilder
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Windows;
using System.Windows.Controls;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.RobotJog.Controls.Joints;

internal class ExternalJointsLayoutBuilder(
  CUiJointsControl jointsControl,
  CApRJRobotJogJointsLogic jointJogApplication) : JointsLayoutBuilder(jointsControl, jointJogApplication)
{
  private ComboBox cbExternalJoints;
  private Border sliderBorder;
  private Border valueBorder;
  private Border percentageBorder;
  private Border lowSoftLimitBorder;
  private Border upperSoftLimitBorder;
  private TxJoint selectedJoint;

  public override void InitGrid()
  {
    base.InitGrid();
    ComboBox comboBox = new ComboBox();
    comboBox.VerticalAlignment = VerticalAlignment.Center;
    this.cbExternalJoints = comboBox;
    this.cbExternalJoints.SelectionChanged += (SelectionChangedEventHandler) ((sender, args) =>
    {
      CUiJointNodeData tag = (args.AddedItems[0] as ComboBoxItem).Tag as CUiJointNodeData;
      this.sliderBorder.Child = (UIElement) tag.SliderControl;
      this.valueBorder.Child = (UIElement) tag.ValueControl;
      this.percentageBorder.Child = (UIElement) tag.PercentageControl;
      this.lowSoftLimitBorder.Child = (UIElement) tag.LowSoftLimitControl;
      this.upperSoftLimitBorder.Child = (UIElement) tag.UpperSoftLimitControl;
      this.selectedJoint = tag.Joint;
    });
    string name = this.jointsControl.jointsGrid.ColumnDefinitions[this.jointsControl.jointsGrid.ColumnDefinitions.Count - 1].Name;
    Border border = new Border();
    border.BorderThickness = new Thickness(1.0, 0.0, 1.0, 1.0);
    border.Child = (UIElement) this.cbExternalJoints;
    Border jointNameBorder = border;
    if (name != ColumnsIDS.Joint)
      jointNameBorder.Margin = new Thickness(0.0, 0.0, -7.0, 0.0);
    this.sliderBorder = new Border()
    {
      Padding = new Thickness(2.0, 4.0, 2.0, 4.0)
    };
    if (name != ColumnsIDS.Steering)
      this.sliderBorder.Margin = new Thickness(0.0, 0.0, -7.0, 0.0);
    this.valueBorder = new Border();
    if (name != ColumnsIDS.Value)
      this.valueBorder.Margin = new Thickness(0.0, 0.0, -7.0, 0.0);
    this.percentageBorder = new Border();
    if (name != ColumnsIDS.Percentage)
      this.percentageBorder.Margin = new Thickness(0.0, 0.0, -7.0, 0.0);
    this.lowSoftLimitBorder = new Border();
    if (name != ColumnsIDS.LowerLimit)
      this.lowSoftLimitBorder.Margin = new Thickness(0.0, 0.0, -7.0, 0.0);
    this.upperSoftLimitBorder = new Border();
    if (name != ColumnsIDS.UpperLimit)
      this.upperSoftLimitBorder.Margin = new Thickness(0.0, 0.0, -7.0, 0.0);
    this.jointsControl.AddRowToGrid((CUiJointNodeData) null, jointNameBorder, this.sliderBorder, this.valueBorder, this.percentageBorder, this.lowSoftLimitBorder, this.upperSoftLimitBorder);
  }

  public override void AditionalGridInit()
  {
    if (this.cbExternalJoints.Items.Count > 0)
    {
      if (this.cbExternalJoints.SelectedItem == null)
        this.cbExternalJoints.SelectedIndex = 0;
      this.jointsControl.IsEnabled = true;
    }
    else
    {
      this.selectedJoint = (TxJoint) null;
      this.jointsControl.IsEnabled = false;
    }
  }

  public override int JointsCountInGrid() => this.cbExternalJoints.Items.Count;

  public override void AddJointToGrid(CUiJointNodeData jointData)
  {
    if (!this.jointJogApplication.SaveExternalJointValue(jointData))
      return;
    ComboBoxItem newItem = new ComboBoxItem();
    newItem.Content = (object) jointData.NiceName;
    newItem.Tag = (object) jointData;
    this.cbExternalJoints.Items.Add((object) newItem);
    if (!TxJoint.op_Equality(jointData.Joint, (ITxObject) this.selectedJoint))
      return;
    this.cbExternalJoints.SelectedItem = (object) newItem;
  }

  public override CUiJointNodeData GetJoinData(FrameworkElement element)
  {
    return (this.cbExternalJoints.SelectedItem as ComboBoxItem).Tag as CUiJointNodeData;
  }
}
